package com.tdtxdcxm.telmanagesys;

import com.tdtxdcxm.telmanagesys.itemdata.PeopleItem;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.ArrayList;
import java.util.List;

public class PeopleInfoConfig {
    private static final String peoplelistfilename = "peopleinfo.ple";

    public static void readData(Context context, List<PeopleItem> list){
        list.clear();
        System.out.println("--------------》》》》这里了！");
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        Preferences preferences = databaseHelper.getPreferences(peoplelistfilename);

        String namelist = preferences.getString("名字","请添加联系人").toString();

        System.out.println(namelist);

        String[] pername = namelist.split(",");
        if(pername[0].equals("")){
            list.add(new PeopleItem("请添加联系人"));
            return;
        }
        for(int i = 0;i < pername.length;i++){
            System.out.println("名字："+pername[i]);
            list.add(new PeopleItem(pername[i]));
        }
    }
    public static String[] readPersonData(Context context,String name){
        String[] personinfo = new String[9];
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        Preferences preferences = databaseHelper.getPreferences(name+".tdtx");

        personinfo[0] = preferences.getString("姓名","");
        personinfo[1] = preferences.getString("电话","");
        personinfo[2] = preferences.getString("座机","");
        personinfo[3] = preferences.getString("邮箱","");
        personinfo[4] = preferences.getString("其他邮箱","");
        personinfo[5] = preferences.getString("工作单位","");
        personinfo[6] = preferences.getString("其他单位","");
        personinfo[7] = preferences.getString("地址","");
        personinfo[8] = preferences.getString("其他地址","");
        return personinfo;
    }
    public static void writeData(Context context, String[] personinfo){
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        Preferences preferences = databaseHelper.getPreferences(peoplelistfilename);

        List<PeopleItem> tlist = new ArrayList<>();
        String namelist;
        namelist = preferences.getString("名字","").toString();
        System.out.println("writedata namelistfile--->"+namelist);

        if(namelist.equals("")){
            preferences.putString("名字",personinfo[0]);
        }
        else
        {
            preferences.putString("名字",namelist+","+personinfo[0]);
        }
        preferences.flushSync();

        DatabaseHelper personfile_databaseHelper = new DatabaseHelper(context);
        Preferences person_preferences = personfile_databaseHelper.getPreferences(personinfo[0]+".tdtx");
        person_preferences.putString("姓名",personinfo[0]);
        person_preferences.putString("电话",personinfo[1]);
        person_preferences.putString("座机",personinfo[2]);
        person_preferences.putString("邮箱",personinfo[3]);
        person_preferences.putString("其他邮箱",personinfo[4]);
        person_preferences.putString("工作单位",personinfo[5]);
        person_preferences.putString("其他单位",personinfo[6]);
        person_preferences.putString("地址",personinfo[7]);
        person_preferences.putString("其他地址",personinfo[8]);

        person_preferences.flushSync();
    }
    public static void writePersonData(Context context, String[] personinfo){
        DatabaseHelper personfile_databaseHelper = new DatabaseHelper(context);
        Preferences person_preferences = personfile_databaseHelper.getPreferences(personinfo[0]+".tdtx");
        person_preferences.putString("姓名",personinfo[0]);
        person_preferences.putString("电话",personinfo[1]);
        person_preferences.putString("座机",personinfo[2]);
        person_preferences.putString("邮箱",personinfo[3]);
        person_preferences.putString("其他邮箱",personinfo[4]);
        person_preferences.putString("工作单位",personinfo[5]);
        person_preferences.putString("其他单位",personinfo[6]);
        person_preferences.putString("地址",personinfo[7]);
        person_preferences.putString("其他地址",personinfo[8]);

        person_preferences.flushSync();
    }

    public static void deleteData(Context context, String personname){
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        Preferences preferences = databaseHelper.getPreferences(peoplelistfilename);

        List<PeopleItem> tlist = new ArrayList<>();
        String namelist;
        namelist = preferences.getString("名字","").toString();
        System.out.println("writedata namelistfile--->"+namelist);

        if(namelist.equals("")){

        }
        else
        {
            String[] names = namelist.split(",");
            StringBuilder stringBuilder = new StringBuilder();
            int k = 0;
            for(int i = 0;i < names.length;i++){
                System.out.println("------>>>>"+stringBuilder.toString());
                if(names[i].equals(personname) == false){
                    if(k != 0){
                        stringBuilder.append(",");
                        stringBuilder.append(names[i]);
                        k++;
                        continue;
                    }
                    stringBuilder.append(names[i]);
                    k++;
                }
            }
            namelist = stringBuilder.toString();

            preferences.putString("名字",namelist);
        }
        preferences.flushSync();

        DatabaseHelper personfile_databaseHelper = new DatabaseHelper(context);
        //Preferences person_preferences = personfile_databaseHelper.getPreferences(personname+".tdtx");
        personfile_databaseHelper.deletePreferences(personname+".tdtx");
        personfile_databaseHelper.removePreferencesFromCache(personname+".tdtx");
    }
}
